package com.wdh.kjs.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author loser
 * @ClassName DateUtil
 * @Description  日期工具类
 * @Date 2016/02/26 15:27
 * @History :
 * <author>
 * <time>
 * <desc>
 */

public class DateUtilChange {

    public static final String DATE_YEAR = "yyyy";
    public static final String DATE_MONTH = "MM";
    public static final String DATE_DAY = "dd";
    public static final String DATE_HOUR = "HH";
    public static final String DATE_MINUTE = "mm";
    public static final String DATE_SECOND = "ss";
    public static final Integer DAY = 0;
    public static final Integer MONTH = 1;

    //下划线
    public static final String SEPARATOR_UNDERLINE = "_";
    //中划线
    public static final String SEPARATOR_HYPHEN = "-";
    //斜杠
    public static final String SEPARATOR_OBLIQUE= "/";
    //冒号
    public static final String SEPARATOR_COLON = ":";
    //空格
    public static final String SEPARATOR_BLANK = " ";

    //默认以-作为日期分隔符  以:作为时间分隔符  日期与时间之间以空格相隔
    public static final Pattern DEFAULT_YEAR_PATTERN  = Pattern.compile("^\\d{4}$");
    public static final DateFormat DEFAULT_YEAR_FORMAT  = new SimpleDateFormat("yyyy");

    public static final Pattern DEFAULT_MONTH_PATTERN = Pattern.compile("^\\d{4}-\\d{2}$");
    public static final DateFormat DEFAULT_MONTH_FORMAT = new SimpleDateFormat("yyyy-MM");

    public static final Pattern DEFAULT_DAY_PATTERN = Pattern.compile("^\\d{4}(-\\d{2}){2}$");
    public static final DateFormat DEFAULT_DAY_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static final DateFormat DEFAULT_DAY_FORMAT_NULLGAN = new SimpleDateFormat("yyyyMMdd");

    public static final Pattern DEFAULT_TIME_PATTERN = Pattern.compile("^\\d{2}(:\\d{2}){2}$");
    public static final DateFormat DEFAULT_TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");

    public static final Pattern DEFAULT_HOUR_PATTERN = Pattern.compile("^\\d{4}(-\\d{2}){2}\\s\\d{2}$");
    public static final DateFormat DEFAULT_HOUR_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH");

    public static final Pattern DEFAULT_MINUTE_PATTERN = Pattern.compile("^\\d{4}(-\\d{2}){2}\\s\\d{2}:\\d{2}$");
    public static final DateFormat DEFAULT_MINUTE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static final Pattern DEFAULT_SECOND_PATTERN = Pattern.compile("^\\d{4}(-\\d{2}){2}\\s\\d{2}(:\\d{2}){2}$");
    public static final DateFormat DEFAULT_SECOND_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date converseToDateIntellectually(String source){

        if(DEFAULT_DAY_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_DAY_FORMAT);
        }else if(DEFAULT_MONTH_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_MONTH_FORMAT);
        }else if(DEFAULT_MINUTE_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_MINUTE_FORMAT);
        }else if(DEFAULT_SECOND_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_SECOND_FORMAT);
        }else if(DEFAULT_TIME_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_TIME_FORMAT);
        }else if(DEFAULT_HOUR_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_HOUR_FORMAT);
        }else if(DEFAULT_YEAR_PATTERN.matcher(source).matches()){
            return string2Date(source, DEFAULT_YEAR_FORMAT);
        }

        return null;
    }

    public static String date2String(Date source, String formatString){
        return date2String(source, new SimpleDateFormat(formatString));
    }

    public static String date2String(Date source, DateFormat format){
        try{
            return format.format(source);
        }catch (Exception e){
            return "";
        }
    }

    public static Date string2Date(String source, DateFormat format){

        Date target = null;

        try {
            target = format.parse(source);
        }catch (Exception e){
            e.printStackTrace();
        }

        return target;
    }

    public static Date string2Date(String source, String formatString){
        return string2Date(source, new SimpleDateFormat(formatString));
    }

    public static Date timestamp2Date(long source){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(source);
        return calendar.getTime();
    }

    public static String timestamp2String(long source, DateFormat format){
        return date2String(timestamp2Date(source), format);
    }

    public static String timestamp2String(long source, String formatString){
        return date2String(timestamp2Date(source), new SimpleDateFormat(formatString));
    }

//    /**
//     * 日期格式字符串转换成时间戳
//     * @param date_str 字符串日期
//     * @param format 如：yyyy-MM-dd HH:mm:ss
//     * @return
//     *
//     *          String timeStamp2 = date2TimeStamp("2014-12-05 23:17:07", "yyyy-MM-dd HH:mm:ss");
//                System.out.println(timeStamp2) timeStamp2 =  1417792627
//     */
//    public static String date2TimeStamp(String date_str,String format){
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat(format);
//            return String.valueOf(sdf.parse(date_str).getTime()/1000);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "";
//    }
    /**
     * 〈方法的功能描述〉:TODO
     * 获取两个日期的相差时间,可为月，可为天,天的比较没问题，月的比较比较局限，只能比较相差多少个月，不能以15日为结点向上下取整
     *
     * @param // date1 日期1，date2 日期2，stype 相差时间，0为天，1为月
     * @return 相差时间
     * @throws [返回类型说明]
     * @Date    // 2016/3/27/16:52
     * @author // lw
     * @methodName compareDate
     * @History:// 历史修改记录
     * <author>  // 修改人1
     * <time>    // 修改时间
     * <desc>    // 描述修改内容
     */
    public static int compareDate(String date1, String date2, int stype) {
        int n = 0;
        String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd";
        DateFormat df = new SimpleDateFormat(formatStyle);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(date1));
            c2.setTime(df.parse(date2));
        } catch (Exception e3) {
            System.out.println("wrong occured");
        }
        if (c2.after(c1)) {
            while (!c1.after(c2)) {                     // 循环对比，直到相等，n 就是所要的结果
                n++;
                if (stype == 1) {
                    c1.add(Calendar.MONTH, 1);          // 比较月份，月份+1
                } else {
                    c1.add(Calendar.DATE, 1);           // 比较天数，日期+1
                }
            }
        } else {
            while (!c2.after(c1)) {
                n++;
                if (stype == 1) {
                    c2.add(Calendar.MONTH, 1);
                } else {
                    c2.add(Calendar.DATE, 1);
                }
            }
        }

        n = n - 1;
        return n;
    }

    /**
     * 比较两个时间节点的差值，beforeDate在afterDate之前，若是在其后，将返回-1
     * @param beforeDate
     * @param afterDate
     * @param stype
     * @return
     */
    public static int compareBeforeAndAfter(String beforeDate, String afterDate, int stype) {
        int n = 0;
        String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd";
        DateFormat df = new SimpleDateFormat(formatStyle);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(beforeDate));
            c2.setTime(df.parse(afterDate));
        } catch (Exception e3) {
            System.out.println("wrong occured");
        }
        if (c2.after(c1)) {
            while (!c1.after(c2)) {                     // 循环对比，直到相等，n 就是所要的结果
                n++;
                if (stype == 1) {
                    c1.add(Calendar.MONTH, 1);          // 比较月份，月份+1
                } else {
                    c1.add(Calendar.DATE, 1);           // 比较天数，日期+1
                }
            }
        }
        n = n - 1;
        return n;
    }


}
